package com.studentManagement.servlet;

import com.studentManagement.dao.ClassDao;
import com.studentManagement.dao.DormitoryDao;
import com.studentManagement.daoImpl.ClassDaoImpl;
import com.studentManagement.daoImpl.DormitoryDaoImpl;
import com.studentManagement.po.Class;
import com.studentManagement.po.Teacher;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Administrator on 2017/4/29 0029.
 */
@WebServlet(name = "InitServlet")
public class InitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");

        if (action.equals("getClass")){
            getClass(request,response); //初始化班级目录
        }else if(action.equals("initClass")){
            initClass(request, response);//初始化studentList
        }
    }
    /**
    * 方法的功能描述：初始化班级列表
    * @params：[request, response]
    * @return:  void*/
    private void getClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //初始配置
        PrintWriter out = response.getWriter();
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        String Teacher_id = teacher.getTeacher_id();
        //通过教师的id查询所管理班级的id
        List<Class> list1 ;
        List<String> list2=new ArrayList<>() ;
        ClassDao query = new ClassDaoImpl();
        list1 = query.queryClassByTeacher_id(Teacher_id);
        for (int i = 0; i < list1.size(); i++) {
            list2.add(list1.get(i).getClass_id());
        }
        //转换为json对象
        JSONArray jsonArray=JSONArray.fromObject(list2);
        out.print(jsonArray);
    }
    /**
     * 方法的功能描述：初始化班级管理
     * @params：[request, response]
     * @return: void */
    private void initClass(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
        String class_id=request.getParameter("class_id");
        request.getSession().setAttribute("class_id",class_id);
        response.sendRedirect(request.getContextPath()+"/jsp/teacher/class/class_index.jsp");
    }
}
